<?php
namespace app\content\controller;

use app\model\Classes;
use app\model\ClassPlan as ModelClassPlan;
use app\model\ClassPlanTask as ModelClassPlanTask;
use app\model\ClassUser;
use app\model\Exampaper;
use app\model\Product;
use app\model\ProductChapter as ModelProductChapter;
use app\model\ProductCourse as ModelProductCourse;
use app\model\User;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 课程视频
 * @class ProductChapter
 * @package app\content\controller
 */
class ProductCourse extends Controller
{
  
    /**
     * 课程视频
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        ModelProductCourse::mQuery()->layTable(function () {
            $this->title = '课程视频';
            $this->chapter_list = ModelProductChapter::mk()->where(['status' => 1])->select();
            $this->product_list = Product::mk()->where(['status' => 1])->select();
        }, function (QueryHelper $query) {

            // 加载对应数据列表
            // $query->where(['is_deleted' => 0, 'status' => intval($this->type === 'index')]);

            // 关联用户身份资料
            /** @var \think\model\Relation|\think\db\Query $query */
            $query->with(['chapter' => static function ($query) {
                $query->field('id,name');
            }, 'product' => static function ($query) {
                $query->field('id,name');
            }]);

            // 数据列表搜索过滤
            $query->equal('parent_id')->dateBetween('create_time');
            $query->like('name');
        });
    }

    /**
     * 添加学习计划详情
     * @auth true
     */
    public function add()
    {
        $this->chapter_list = ModelProductChapter::mk()->where(['status' => 1])->select();
        $this->product_list = Product::mk()->where(['status' => 1])->select();
        ModelProductCourse::mForm('form');
    }

    /**
     * 编辑学习计划详情
     * @auth true
     */
    public function edit()
    {
        $this->chapter_list = ModelProductChapter::mk()->where(['status' => 1])->select();
        $this->product_list = Product::mk()->where(['status' => 1])->select();
        ModelProductCourse::mForm('form');
    }



    /**
     * 修改
     * @auth true
     */
    public function pass()
    {
        $this->_applyFormToken();
        if ($this->request->isGet()) {
            $this->verify = false;
            ModelProductCourse::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require'                  => '用户ID不能为空！',
             
            ]);
            $ModelClassPlan = ProductCourse::mk()->findOrEmpty($data['id']);
            if ($ModelClassPlan->isExists() && $ModelClassPlan->save([])) {
             
                $this->success('');
            } else {
                $this->error('');
            }
        }
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        
    }

    /**
     * 修改学习计划详情状态
     * @auth true
     */
    public function state()
    {
        $this->_checkInput();
        ModelProductCourse::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除学习计划详情
     * @auth true
     */
    public function remove()
    {
        $this->_checkInput();
        ModelProductCourse::mDelete();
    }

    /**
     * 检查输入变量
     */
    private function _checkInput()
    {
       
    }

    
    
}
